<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title></title>
        <link rel="stylesheet" href="zhucedenglu.css" type="text/css">
	<script src="jquery.validate.min.js"></script>

</head>
<body>
<div id="mask">
	<table>
		<div class="drog">
			<h3 align="center">
				注册
			</h3>
			<form  id="formId">
				<p align="center">
					用户名:<input type="text" id="username"/>
				</p>
				<p align="center">
					email:<input type="email" id="email"/>
				</p>
				<p align="center">
					密&emsp;码:<input type="password" id="password"/>
				</p>
				<p align="center">
					确认密码:<input type="password" id="repassword"/>
				</p>
			</form>

			<p align="center">
				<button id="regist" type="submit">注册</button>
			</p>
			<p align="center">
				已有账号？<a href="1login.html">请登录</a>
			</p>
		</div>
	</table>
</div>

<script>
	$(document).ready(function () {
		$("#formId").validate({
			rules: {
				username: {
					required: true,
					maxlength: 12,
					minlength: 6
				},
				pwd: {
					required: true,
					maxlength: 8,
					minlength: 6,
				},
				pwd_confirm: {
					equalTo: "#pwd"
				},
				email: {
					email: true
				}
			},
			messages: {
				username: {
					required: "名称必须填写",
					maxlength: "最大长度为{12}",
					minlength: "最小长度为{6}"
				},
				pwd: {
					required: "密码必须填写",
					maxlength: "最大长度为{8}",
					minlength: "最小长度为{6}",
				},
				pwd_confirm: {
					equalTo: "两次密码不同"
				}
			}
		});
	})
</script>

<script>
	//先获取所有用户的对象//变成数组
	if(window.localStorage.userArr){//判断是否存在
		var array = JSON.parse(window.localStorage.userArr);
	}else{
		array = [];//创建一个新数组
	}


	//注册
	document.getElementById('regist').onclick = function(){
		var username = document.getElementById('username').value;
		var email = document.getElementById('email').value;
		var password = document.getElementById('password').value;
		var repassword = document.getElementById('repassword').value;
		//遍历数组进行匹配
		for(var i =0;i<array.length;i++){
			//判断是否有相同账号
			if(username==array[i].username){
				alert("该账号已存在");
				window.location.href='1login.html';
				  return;
			}
		}
		//创建对象
		var obj = {username:username,email:email,password:password,repassword:repassword,score:0}
		array.push(obj);
		window.localStorage.userArr=JSON.stringify(array);
		alert("用户创建成功");

	}

</script>
</body>
</html>